package function;

import model.Author;
import model.Book;
import model.GetAuthors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Collect {
    public static void main1(String[] args) {
        List<Author> authors = GetAuthors.getAuthors();
        List<String> names = authors.stream()
                .map(author -> author.getName())
                .distinct()
                .collect(Collectors.toList());
        System.out.println(names);

    }

    public static void main2(String[] args) {
        List<Author> authors = GetAuthors.getAuthors();
        Set<String> set = authors.stream()
                .flatMap(author -> author.getBooks().stream())
                .distinct()
                .map(book -> book.getName())
                .collect(Collectors.toSet());
        System.out.println(set);


    }


    public static void main(String[] args) {
        List<Author> authors = GetAuthors.getAuthors();
        Map<String, List<Book>> collect = authors.stream()
                .distinct()
                .collect(Collectors.toMap(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }, new Function<Author, List<Book>>() {
                    @Override
                    public List<Book> apply(Author author) {
                        return author.getBooks();
                    }
                }));
        System.out.println(collect);
    }
}
